package com.open.capacity.salary.controller;

import java.util.*;

import com.open.capacity.annotation.log.LogAnnotation;
import com.open.capacity.commons.Result;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.salary.dto.SysZtUserVo;
import com.open.capacity.salary.entity.GzZtUser;
import com.open.capacity.user.model.SysUserRelation;
import com.open.capacity.user.service.SysUserService;
import com.open.capacity.user.vo.SysUserRelationVo;
import com.open.capacity.vo.SysUserRelationExtVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import com.open.capacity.salary.service.GzZtUserService;

import com.open.capacity.commons.PageResult;


/**
 * 工资系统-账套下的职工
 *
 * @author
 * @email
 * @date 2021-08-20 15:07:33
 */
@RestController
@RequestMapping("gzztusers")
@Api(tags = "工资系统-账套下的职工")
public class GzZtUserController {

    @Autowired
    private GzZtUserService gzZtUserService;


    @Autowired
    private SysUserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('gz_zt_user:gzztuser:list')")
    public Result<SysUserRelationExtVo> list(String companyId, String ztId, String keyword) {
        SysUserRelationExtVo vo = new SysUserRelationExtVo();
        List<GzZtUser> list = gzZtUserService.findByZtId(companyId, ztId);
        String userIds = "";
        List<SysUser> userSet = new ArrayList<>();
        for (GzZtUser item : list) {
            if (userIds != "") {
                userIds += ",";
            }
            userIds += item.getUserId();
            SysUser u = userService.findById(item.getUserId());
            if (StringUtils.isEmpty(keyword) || (u.getRealName().contains(keyword) || u.getUserCode().contains(keyword))) {
                userSet.add(u);
            }
        }
        vo.setUserInfoList(userSet);
        vo.setUserIds(userIds);
        return Result.succeed(vo);
    }


    /**
     * 查询单条记录
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('gz_zt_user:gzztuser:list')")
    public Result findById(String id) {
        return Result.succeed(gzZtUserService.findById(id));
    }

    /**
     * 保存
     */
    @RequestMapping("/saveOrUpdate")
    @PreAuthorize("hasAnyAuthority('gz_zt_user:gzztuser:save')")
    public Result save(String companyId, @RequestBody SysZtUserVo userVo) {
        gzZtUserService.save(companyId, userVo);
        return Result.succeed("保存成功");
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('gz_zt_user:gzztuser:delete')")
    public Result delete(@PathVariable String id) {
        gzZtUserService.delete(id);
        return Result.succeed("删除成功");
    }

}
